package com.ruoyi.web.controller.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.controller.demo.domain.SysEmail;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.mapper.SysEmailMapper;
import com.ruoyi.web.controller.service.ISysEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.text.Convert;

/**
 * sys_emailService业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-22
 */
@Service
public class SysEmailServiceImpl implements ISysEmailService
{
    @Autowired
    private SysEmailMapper sysEmailMapper;

    /**
     * 查询sys_email
     *
     * @param id sys_email主键
     * @return sys_email
     */
    @Override
    public SysEmail selectSysEmailById(Long id)
    {
        return sysEmailMapper.selectSysEmailById(id);
    }

    /**
     * 查询sys_email列表
     *
     * @param sysEmail sys_email
     * @return sys_email
     */
    @Override
    public List<SysEmail> selectSysEmailList(SysEmail sysEmail)
    {
        return sysEmailMapper.selectSysEmailList(sysEmail);
    }

    @Override
    public List<SysEmail> selectSysEmailListShow(SysEmail sysEmail) {
        return sysEmailMapper.selectSysEmailListShow(sysEmail);
    }

    @Override
    public List<SysEmail> selectSysEmailListDraft(SysEmail sysEmail) {
        return sysEmailMapper.selectSysEmailListDraft(sysEmail);
    }

    @Override
    public List<SysEmail> selectSysEmailListDash(SysEmail sysEmail) {
        return sysEmailMapper.selectSysEmailListDash(sysEmail);
    }

    /**
     * 新增sys_email
     *
     * @param sysEmail sys_email
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysEmail(SysEmail sysEmail)
    {
        sysEmail.setCreateTime(DateUtils.getNowDate());
        int rows = sysEmailMapper.insertSysEmail(sysEmail);
        insertSysWzxUser(sysEmail);
        return rows;
    }
/*
    @Override
    public int insertSysEmailIn(SysEmail sysEmail) {
        return sysEmailMapper.insertSysEmailIn(sysEmail);
    }*/

    @Override
    public int saveSysEmail(SysEmail sysEmail) {
        return sysEmailMapper.saveSysEmail(sysEmail);
    }

    /**
     * 修改sys_email
     *
     * @param sysEmail sys_email
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysEmail(SysEmail sysEmail)
    {
        sysEmailMapper.deleteSysWzxUserByUserId(sysEmail.getId());
        insertSysWzxUser(sysEmail);
        return sysEmailMapper.updateSysEmail(sysEmail);
    }

    /**
     * 批量删除sys_email
     *
     * @param ids 需要删除的sys_email主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysEmailByIds(String ids)
    {
        sysEmailMapper.deleteSysWzxUserByUserIds(Convert.toStrArray(ids));
        return sysEmailMapper.deleteSysEmailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除sys_email信息
     *
     * @param id sys_email主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysEmailById(Long id)
    {
        sysEmailMapper.deleteSysWzxUserByUserId(id);
        return sysEmailMapper.deleteSysEmailById(id);
    }

    @Override
    public int outDeleteSysEmailById(Long id) {
        return sysEmailMapper.outDeleteSysEmailById(id);
    }

    @Override
    public int shouDeleteSysEmailById(Long id) {
        return sysEmailMapper.shouDeleteSysEmailById(id);
    }

    @Override
    public int backRemove(Long id) {
        return sysEmailMapper.backRemove(id);
    }

    /**
     * 新增人员信息信息
     *
     * @param sysEmail sys_email对象
     */
    public void insertSysWzxUser(SysEmail sysEmail)
    {
        List<SysWzxUser> sysWzxUserList = sysEmail.getSysWzxUserList();
        Long id = sysEmail.getId();
        if (StringUtils.isNotNull(sysWzxUserList))
        {
            List<SysWzxUser> list = new ArrayList<SysWzxUser>();
            for (SysWzxUser sysWzxUser : sysWzxUserList)
            {
                sysWzxUser.setUserId(id);
                list.add(sysWzxUser);
            }
            if (list.size() > 0)
            {
                sysEmailMapper.batchSysWzxUser(list);
            }
        }
    }
}
